package ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.chaychan.library.UIUtils;
import com.fy.fyzf.MainActivity;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.base.BaseApp;
import com.fy.fyzf.base.BasePresenter;
import com.fy.fyzf.base.Contans;
import com.fy.fyzf.listener.PermissionListener;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.SPUtils;

import java.util.List;

import kotlin.jvm.Volatile;


public class SplashActivity extends BaseActivity {


    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_splash2;
    }

    @Override
    public void initData() {
        super.initData();

        requestRuntimePermission(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CALL_PHONE}, new PermissionListener() {
            @Override
            public void onGranted() {
                initLocation();
                toNext();
            }

            @Override
            public void onDenied(List<String> deniedPermissions) {
//                UIUtils.showToast(getString(R.string.write_storage_permission_deny));
//                AppUtils.showToast("权限被拒绝，可能会影响功能正常使用");
                toNext();
            }
        });

    }

    private void toNext() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String token = SPUtils.getInstance(BaseApp.getContext()).getString("author", "");
                if (token == null || token.equals("")) {
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                } else {
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                }

                finish();
            }
        }).start();
    }

    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {
                if (amapLocation.getErrorCode() == 0) {
                    Contans.latitude = amapLocation.getLatitude();
                    Contans.longitude = amapLocation.getLongitude();
                } else {
                }
            }
        }
    };
    //声明mlocationClient对象
    public AMapLocationClient mlocationClient;
    //声明mLocationOption对象
    public AMapLocationClientOption mLocationOption = null;
    //获取经纬度
    private void initLocation() {
        mlocationClient = new AMapLocationClient(this);
        mLocationOption = new AMapLocationClientOption();
        mlocationClient.setLocationListener(mLocationListener);
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setOnceLocation(true);
        mlocationClient.setLocationOption(mLocationOption);
        mlocationClient.startLocation();
    }

    @Override
    public void initView() {
        super.initView();

    }
}